/*****************************************************************************
Copyright (c) 2011 Nokia Siemens Networks. All rights reserved.

This software is proprietary to and embodies the confidential technology 
of Nokia Siemens Networks. Possession, use, or copying of this software 
and media is authorized only pursuant to a valid written license from 
Nokia Siemens Networks.
 *****************************************************************************/
package com.fashion.web.actions.management.gallery;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.gallery.Album;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.gallery.PhotoService;
import com.fashion.utils.Variable;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Description TODO
 * 
 * <br>
 * <b>Change Note:</b>
 * <ul>
 * <li>Date:</li>
 * <li>Modifier:</li>
 * <li>Description:</li>
 * </ul>
 * 
 * @author TODO created at Jul 11, 2011
 */
@Controller
@ParentPackage("json-default")
@Namespace("/management/gallery")
public class CoverAdminAction extends ActionSupport {
	@Resource(name = "photoService")
	private PhotoService photoService;
	@Resource(name = "albumService")
	private AlbumService albumService;
	private String coverType;
	private String coverFolder;
	private String aspectRatio;
	private Rectangle area;
	private Album album;

	@Action(value = "tailor_cover", results = { @Result(name = SUCCESS, location = "album_index", type = "redirectAction", params={"album.id", "${album.id}"}) })
	public String tailorCover() throws Exception {
		int coverMiniSize = 0;
		if ("TRANSVERSE_COVER".equals(coverType)) {
			coverFolder = Variable.TRANSVERSE_COVER_DIR_SUBSTR;
			coverMiniSize = Variable.TRANSVERSE_COVER_MINI_SIZE;
		} else {
			coverFolder = Variable.LOGNITUDINAL_COVER_DIR_SUBSTR;
			coverMiniSize = Variable.LOGNITUDINAL_COVER_SIZE;
		}

		album = albumService.findById(album.getId());
		String pre = ServletActionContext.getServletContext().getRealPath(
				Variable.PHOTO_DIR_SUBSTR);
		String coverPath = pre + File.separator + album.getId()
				+ File.separator + Variable.COVER_DIR_SUBSTR + File.separator + coverFolder
				+ File.separator + album.getCover();
		String coverMiniFolder = pre + File.separator + album.getId()
				+ File.separator + Variable.COVER_DIR_SUBSTR + File.separator + coverFolder
				+ File.separator + Variable.COVER_MINI_DIR_SUBSTR;
		photoService.tailorPhoto(coverPath, area.x, area.y, area.width,
				area.height);

		photoService.savePhoto(new FileInputStream(coverPath),
				album.getCover(), coverMiniFolder, coverMiniSize);
		return SUCCESS;
	}

	@Action(value = "cover_index", results = { @Result(name = SUCCESS, location = "cover.jsp") })
	public String index() throws Exception {
		album = albumService.findById(album.getId());

		if ("TRANSVERSE_COVER".equals(coverType)) {
			aspectRatio = Variable.TRANSVERSE_ASPECT_RATIO;
			coverFolder = Variable.TRANSVERSE_COVER_DIR_SUBSTR;

		} else {
			aspectRatio = Variable.LOGNITUDINAL_ASPECT_RATIO;
			coverFolder = Variable.LOGNITUDINAL_COVER_DIR_SUBSTR;
		}
		return SUCCESS;
	}

	public String getAspectRatio() {
		return aspectRatio;
	}

	public void setAspectRatio(String aspectRatio) {
		this.aspectRatio = aspectRatio;
	}

	public Rectangle getArea() {
		return area;
	}

	public void setArea(Rectangle area) {
		this.area = area;
	}

	public void setCoverType(String coverType) {
		this.coverType = coverType;
	}

	public String getCoverType() {
		return coverType;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public String getCoverFolder() {
		return coverFolder;
	}

	public Album getAlbum() {
		return album;
	}

}
